# Maintainer: Martin Diehl <aur@martin-diehl.net>
# Contributor: Alad Wenter <alad@archlinux.org>
# Contributor: Jingbei Li <i@jingbei.li>
# Contributor: Simon Pintarelli <simon.pintarelli@gmail.com>
# Contributor: Feng Wang <wanng.fenng@gmail.com>
# Contributor (msys): Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=trilinos
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=14.4.0
_pkgver=${pkgver//./-}
pkgrel=1
pkgdesc='Algorithms for the solution of large-scale scientific problems" (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-matio"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-perl"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-bc"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "unzip")
optdepends=("${MINGW_PACKAGE_PREFIX}-seacas")
license=('LGPL3')
url="http://trilinos.org"
source=("https://github.com/${_realname}/Trilinos/archive/refs/tags/${_realname}-release-${_pkgver}.tar.gz")
sha256sums=('8E7D881CF6677AA062F7BFEA8BAA1E52E8956AA575D6A4F90F2B6F032632D4C6')
noextract=(${_realname}-release-${_pkgver}.tar.gz)

prepare(){
  plain "Extract ${_realname}-release-${_pkgver}.tar.gz"
  tar -xzf ${_realname}-release-${_pkgver}.tar.gz || true
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config[@]} \
      -DBUILD_SHARED_LIBS=ON \
      -DBLAS_LIBRARY_NAMES="openblas" \
      -DLAPACK_LIBRARY_NAMES="openblas" \
      -DTrilinos_ENABLE_ALL_OPTIONAL_PACKAGES=ON \
      -DTrilinos_ENABLE_ALL_PACKAGES=ON \
      -DTrilinos_ENABLE_Fortran=ON \
      -DTrilinos_ENABLE_SEACAS=OFF \
      -DTrilinos_ENABLE_Zoltan=OFF \
      -DTrilinos_ENABLE_Teuchos=OFF \
      -DTrilinos_ENABLE_Epetra=OFF \
      ../Trilinos-${_realname}-release-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/Trilinos-${_realname}-release-${_pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
